package fh.app4stud.mensa.activities;

import android.app.TabActivity;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import fh.app4stud.mensa.R;
import fh.app4stud.mensa.database.MenuDbAdapter;

import java.util.ArrayList;

/**
 * Tabed view of menus
 * 
 * Created by IntelliJ IDEA. User: ivan plese Date: 22.04.11 Time: 07:25
 */
public class MenusTabWidget extends TabActivity {

	private String currentLocation;
	private int currentDayId;
	private ArrayList<String> tab1ItemList;
	private ArrayList<String> tab2ItemList;
	private ArrayList<String> tab3ItemList;
	private ArrayList<String> tab4ItemList;
	private ListView listView1;
	private ListView listView2;
	private ListView listView3;
	private ListView listView4;
	private MenuDbAdapter mDbHelper;
	private Cursor mCursor;

	private String TAG = "TAB WIDGET TAG";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menus_tab_widget);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			currentLocation = extras
					.getString(SettingsPreferences.CURRENT_LOCATION);
			currentDayId = extras.getInt(SettingsPreferences.CURRENT_DAY);

			mDbHelper = new MenuDbAdapter(this);
			mDbHelper.open();
		}

		tab1ItemList = new ArrayList<String>();
		tab2ItemList = new ArrayList<String>();
		tab3ItemList = new ArrayList<String>();
		tab4ItemList = new ArrayList<String>();

		listView1 = (ListView) findViewById(R.id.tab_list1);
		listView2 = (ListView) findViewById(R.id.tab_list2);
		listView3 = (ListView) findViewById(R.id.tab_list3);
		listView4 = (ListView) findViewById(R.id.tab_list4);

		Resources res = getResources(); // Resource object to get Drawables
		TabHost tabHost = getTabHost(); // The activity TabHost
		TabHost.TabSpec spec; // Reusable TabSpec for each tab

		// set general info about the menu and add items to the items list
		fillData();

		listView1.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, tab1ItemList));
		tabHost.addTab(tabHost
				.newTabSpec("1")
				.setIndicator("Classic",
						res.getDrawable(R.drawable.ic_tab_menu3_grey))
				.setContent(new TabHost.TabContentFactory() {
					public android.view.View createTabContent(String arg0) {
						return listView1;
					}
				}));

		listView2.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, tab2ItemList));
		tabHost.addTab(tabHost
				.newTabSpec("2")
				.setIndicator("Daily Menu",
						res.getDrawable(R.drawable.ic_tab_menu2_grey))
				.setContent(new TabHost.TabContentFactory() {
					public android.view.View createTabContent(String arg0) {
						return listView2;
					}
				}));

		listView3.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, tab3ItemList));
		tabHost.addTab(tabHost
				.newTabSpec("3")
				.setIndicator("Brain Food",
						res.getDrawable(R.drawable.ic_tab_menu1_grey))
				.setContent(new TabHost.TabContentFactory() {
					public android.view.View createTabContent(String arg0) {
						return listView3;
					}
				}));

		listView4.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, tab4ItemList));
		tabHost.addTab(tabHost
				.newTabSpec("4")
				.setIndicator("Cafe Mensa",
						res.getDrawable(R.drawable.ic_tab_menu4_grey))
				.setContent(new TabHost.TabContentFactory() {
					public android.view.View createTabContent(String arg0) {
						return listView4;
					}
				}));
		tabHost.setCurrentTab(0);
	}

	private void fillData() {

		if (currentLocation != null) {

			// perform the query on the data base
			mCursor = mDbHelper.rawQueryByDayAndLoc(currentLocation,
					currentDayId);

			// if the query gave back results
			if (mCursor != null) {
				// we use the setTab bool to set the general info of the intent
				// only once
				boolean setTab1 = false;
				boolean setTab2 = false;
				boolean setTab3 = false;
				boolean setTab4 = false;

				// get the index for the data base tables
				int dayNameKey = mCursor
						.getColumnIndexOrThrow(MenuDbAdapter.KEY_DAY_NAME);
				int menuIdKey = mCursor
						.getColumnIndexOrThrow(MenuDbAdapter.KEY_MENU_ID);
				int menuNameKey = mCursor
						.getColumnIndexOrThrow(MenuDbAdapter.KEY_MENU_NAME);
				int menuPriceKey = mCursor
						.getColumnIndexOrThrow(MenuDbAdapter.KEY_PRICE);
				int itemKey = mCursor
						.getColumnIndexOrThrow(MenuDbAdapter.KEY_ITEM);

				if (mCursor.moveToFirst()) {

					while (!mCursor.isAfterLast()) {

						// get the data from cursor at its current position
						int menuId = Integer.parseInt(mCursor
								.getString(menuIdKey));
						String dayName = mCursor.getString(dayNameKey);
						String menuName = mCursor.getString(menuNameKey);
						String menuPrice = mCursor.getString(menuPriceKey);
						String item = mCursor.getString(itemKey);

						switch (menuId) {
						// Classic menu
						case 1:
							// set the general info only once
							if (!setTab1) {
								tab1ItemList.add(menuName);
								tab1ItemList.add(menuPrice);
								setTab1 = true;
							}
							// add items to list that will be used to show items
							// in tabs
							tab1ItemList.add(item);
							break;
						// Daily Menu
						case 2:
							if (!setTab2) {
								tab2ItemList.add(menuName);
								tab2ItemList.add(menuPrice);
								setTab2 = true;
							}
							tab2ItemList.add(item);
							break;
						// Brain Food
						case 3:
							if (!setTab3) {
								tab3ItemList.add(menuName);
								tab3ItemList.add(menuPrice);
								setTab3 = true;
							}
							tab3ItemList.add(item);
							break;
						// Cafe Mensa
						case 4:
							if (!setTab4) {
								tab4ItemList.add(menuName);
								tab4ItemList.add(menuPrice);
								setTab4 = true;
							}
							tab4ItemList.add(item);
							break;
						}

						mCursor.moveToNext();
					}
				}
			}
			mDbHelper.close();
		}
	}
}
